<?php

namespace app\adminapi\controller;

use app\common\service\Config as ConfigService;

use support\Request;
use support\think\Cache;
use app\BaseController as Controller;

/**
 * 公共的方法
 */
class CommonController extends Controller
{
    /**
     * 验证码
     */
    public function captcha(Request $request)
    {
        $param['api'] = true;
        $captcha = \app\common\service\Verify::entry($param);

        return api_response(config('constants.STATUS_SUCCESS'), '', [
            'key' => $captcha['key'],
            'src' => $captcha['data'],
            'switch' => true, // 是否启用
        ]);
    }

    /**
     * 设置
     */
    public function setting(Request $request)
    {
        $key = 'system_setting';
        if ($request->isPost()) {
            $param = $request->all();
            $value = [
                'favicon_url' => $param['favicon_url'],
                'login_bg_url' => $param['login_bg_url'],
                'login_logo_url' => $param['login_logo_url'],
                'logo_url' => $param['logo_url'],
                'page_title' => $param['page_title'],
                'system_name' => $param['system_name'],
            ];
            $result = ConfigService::setOption($key, $key, $value);
            if ($result === false) {
                return api_response(config('constants.STATUS_ERROR'), '设置失败！', []);
            }

            return api_response(config('constants.STATUS_SUCCESS'), '成功', $value);
        }
        $info = ConfigService::getOption($key, $key);
        if (!$info) {
            $info = [
                'system_name' => '后台管理系统',
                'login_logo_url' => '',
                'login_bg_url' => asset('static/admin/img/login_bg.jpg'),
                'page_title' => 'VueAdmin系统',
                'logo_url' => '',
                'favicon_url' => '',
            ];
        }

        return api_response(config('constants.STATUS_SUCCESS'), '', $info);
    }

    /**
     * 清除缓存
     */
    public function delCache(Request $request)
    {
        // 清除admin标签的缓存数据
        Cache::tag('admin')->clear();

        return api_response(config('constants.STATUS_SUCCESS'), '清除缓存成功！', []);
    }

    /**
     * 文件上传
     */
    public function upload(Request $request)
    {
        $respJson = \app\common\service\Uploader::upload($request);
        $result = json_decode($respJson->rawBody(), true);
        if ($result['code'] != 1) {
            return api_response(config('constants.STATUS_ERROR'), $result['msg'], '');
        }
        $data = [
            'name' => $result['filename'],
            'src' => $result['full_filepath'],
            'size' => $result['filesize'],
            'date' => date('Y-m-d H:i:s'),
            //'creator_id' => $this->admin['id'],
            'filepath' => $result['filepath'],
            'full_filepath' => $result['full_filepath'],
            //'result' => $result
        ];

        return api_response(config('constants.STATUS_SUCCESS'), '成功！', $data);
    }

    /**
     * 文件下载
     */
    public function download(Request $request)
    {
        $file_url = $request->input('file');
        $file_url = urldecode($file_url);
        $file_name = basename($file_url);
        // 只能下载本地文件
        return response()->download($file_url, $file_name);
    }

    /**
     * 获取地区
     */
    public function getRegion(Request $request)
    {
        $param = $request->all();
        $where = [
            ['name', '<>', '市辖区']
        ];
        if (!empty($param['parent_code'])) {
            $where[] = ['parent_code', '=', $param['parent_code']];
        } elseif (isset($param['level']) && strlen($param['level']) > 0) {
            $where[] = ['level', '<', $param['level']];
        } else {
            $where[] = ['level', '<', 3];
        }
        $model = new \app\common\model\Region();

        $list = $model->where($where)->field('parent_code, area_code, name, level')->select()->toArray();
        if (!empty($param['parent_code'])) {
            // 动态查询
            $tree = $list;
        } else {
            // 一次性查询完成
            $tree = list_to_tree($list, 'area_code', 'parent_code', 'children', 0);
        }

        return api_response(config('constants.STATUS_SUCCESS'), '', $tree);
    }
}
